package com.util;


import com.exception.CustomerException;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by zxd on 2021/12/11 14:17
 */
public class WebUtils {


    /**
     * 拷贝请求的数据到指定的javabean中
     * @param request
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T  requestToBean(HttpServletRequest request,Class<T> clazz){

        try {
            //1.反射创建对象
            T bean = clazz.newInstance();

            //2.获取请求参数
            Map<String, String[]> parameterMap = request.getParameterMap();
            //3.拷贝
            BeanUtils.populate(bean,parameterMap);

            return bean;

        }catch (Exception e){
            e.printStackTrace();
            throw new CustomerException("拷贝失败!");
        }

    }

}
